package top.chmin.project.cloud.sys.config.info;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import top.chmin.project.cloud.common.*;
import top.chmin.project.cloud.tool.LocalTool;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;



/**
 * chmin
 * 2024-01-25 14:19:00
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class ConfigInfo extends Model {
    /*###########auto#start###########*/
    // 配置编号
    private String configCode;
    // 配置名称
    private String configName;
    // java项目路径
    private String javaPath;
    // vue项目路径
    private String vuePath;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public ConfigInfoModelCondition forInsert() {
        return new ConfigInfoModelCondition().forInsert();
    }

    public ConfigInfoModelCondition forUpdate() {
        return new ConfigInfoModelCondition().forUpdate();
    }

    public ConfigInfoModelCondition forDelete() {
        return new ConfigInfoModelCondition().forDelete();
    }

    public ConfigInfoModelCondition forSelect() {
        return new ConfigInfoModelCondition().forSelect();
    }

    public class ConfigInfoModelCondition extends Condition {
        ConfigInfoModelCondition parent = null;

        private ConfigInfoModelCondition() {
        }

        private ConfigInfoModelCondition(ConfigInfoModelCondition condition) {
            this.parent = condition;
        }

        private ConfigInfoModelCondition forInsert() {
            ConfigInfoModelCondition condition = new ConfigInfoModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();

            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_config_info(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private ConfigInfoModelCondition forUpdate() {
            ConfigInfoModelCondition condition = new ConfigInfoModelCondition();
            /*###########auto#start###########*/
            if (configCode != null) {
                condition.getUpdateList().add("config_code = ?");
                condition.getUpdateValueList().add(configCode);
            }

            if (configName != null) {
                condition.getUpdateList().add("config_name = ?");
                condition.getUpdateValueList().add(configName);
            }

            if (javaPath != null) {
                condition.getUpdateList().add("java_path = ?");
                condition.getUpdateValueList().add(javaPath);
            }

            if (vuePath != null) {
                condition.getUpdateList().add("vue_path = ?");
                condition.getUpdateValueList().add(vuePath);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_config_info#update##where##order##limit#");
            return condition;
        }

        private ConfigInfoModelCondition forDelete() {
            ConfigInfoModelCondition condition = new ConfigInfoModelCondition();
            condition.setSql("DELETE FROM sys_config_info#where##group##order##limit#");
            return condition;
        }

        private ConfigInfoModelCondition forSelect() {
            ConfigInfoModelCondition condition = new ConfigInfoModelCondition();
            condition.modelClass = ConfigInfo.class;
            condition.setSql("SELECT #select# FROM sys_config_info#where##group##order##limit#");
            return condition;
        }

        public ConfigInfoModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public ConfigInfoModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public ConfigInfoModelCondition sub() {
            return new ConfigInfoModelCondition(this);
        }

        public ConfigInfoModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public ConfigInfoCondition toFlex() {
            ConfigInfoCondition flex = new ConfigInfoCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public ConfigCode configCode() {
            return new ConfigCode();
        }

        public class ConfigCode extends StringModelColumn<ConfigInfoModelCondition> {
            private ConfigCode() {
                super(ConfigInfoModelCondition.this, "config_code", ConfigInfo.this.getConfigCode());
            }
        }

        public ConfigName configName() {
            return new ConfigName();
        }

        public class ConfigName extends StringModelColumn<ConfigInfoModelCondition> {
            private ConfigName() {
                super(ConfigInfoModelCondition.this, "config_name", ConfigInfo.this.getConfigName());
            }
        }

        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<ConfigInfoModelCondition> {
            private CreateBy() {
                super(ConfigInfoModelCondition.this, "create_by", ConfigInfo.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<ConfigInfoModelCondition> {
            private CreateTime() {
                super(ConfigInfoModelCondition.this, "create_time", ConfigInfo.this.getCreateTime());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<ConfigInfoModelCondition> {
            private Id() {
                super(ConfigInfoModelCondition.this, "id", ConfigInfo.this.getId());
            }
        }

        public JavaPath javaPath() {
            return new JavaPath();
        }

        public class JavaPath extends StringModelColumn<ConfigInfoModelCondition> {
            private JavaPath() {
                super(ConfigInfoModelCondition.this, "java_path", ConfigInfo.this.getJavaPath());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<ConfigInfoModelCondition> {
            private UpdateBy() {
                super(ConfigInfoModelCondition.this, "update_by", ConfigInfo.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<ConfigInfoModelCondition> {
            private UpdateTime() {
                super(ConfigInfoModelCondition.this, "update_time", ConfigInfo.this.getUpdateTime());
            }
        }

        public VuePath vuePath() {
            return new VuePath();
        }

        public class VuePath extends StringModelColumn<ConfigInfoModelCondition> {
            private VuePath() {
                super(ConfigInfoModelCondition.this, "vue_path", ConfigInfo.this.getVuePath());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
